package com.cndym.utils;

import java.util.Arrays;

public class Combination {

	private int[] index;

	private int length;

	private int n;

	private long numLeft;

	private long total;

	public Combination(int length) {
		this(length, 0);
	}

	public Combination(int length, int n) {
		this.length = length;
		this.n = n;
		reset();
	}

	public void reset() {

		if (n > length) {
			System.exit(1);
		}

		setTotal(numLeft = (int) Math.pow((double) 2, (double) length));

		index = new int[length];
		Arrays.fill(index, 0);
	}

	private int sum() {
		int s = 0;
		for (int i : index) {
			s += i;
		}
		return s;
	}

	public boolean hasMore() {
		return numLeft > 0;
	}

	public int[] getNext() {

		index[0] += 1;
		for (int i = 0; i < index.length; i++) {
			if (index[i] == 2) {
				index[i] = 0;
				if (i != index.length - 1)
					index[i + 1] += 1;
			}
		}
		numLeft--;
		if (this.n != 0) {
			if (sum() == this.n)
				return index;
			else if (hasMore())
				return getNext();
		}
		return index;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public long getTotal() {
		return total;
	}
}